package com.xjy.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.xjy.common.core.page.PagePlus;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.common.utils.PageUtils;
import com.xjy.system.domain.FundLedgerDetail;
import com.xjy.system.domain.ReportLoanRecord;
import com.xjy.system.domain.bo.FundLedgerDetailQueryBo;
import com.xjy.system.domain.vo.FundLedgerDetailVo;
import com.xjy.system.mapper.FundLedgerDetailMapper;
import com.xjy.system.service.IFundLedgerDetailService;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 资金流水明细报表Service业务层处理
 *
 * @author mlb
 * @date 2022-03-26
 */
@Service
public class FundLedgerDetailServiceImpl extends ServicePlusImpl<FundLedgerDetailMapper, FundLedgerDetail> implements IFundLedgerDetailService {

    @Override
    public FundLedgerDetailVo queryById(Date transactionDate){
        return getVoById(transactionDate, FundLedgerDetailVo.class);
    }

    @Override
    public TableDataInfo<FundLedgerDetailVo> queryPageList(FundLedgerDetailQueryBo bo) {
        PagePlus<FundLedgerDetail, FundLedgerDetailVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), FundLedgerDetailVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<FundLedgerDetailVo> queryList(FundLedgerDetailQueryBo bo) {
        return listVo(buildQueryWrapper(bo), FundLedgerDetailVo.class);
    }

    private LambdaQueryWrapper<FundLedgerDetail> buildQueryWrapper(FundLedgerDetailQueryBo bo) {
		String date = bo.getTransactionDate();
		Calendar cld = Calendar.getInstance();
		try {
			cld.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cld.add(Calendar.MONTH,1);
		cld.set(Calendar.DAY_OF_MONTH,1);
		cld.add(Calendar.DATE,-1);
		String endDate = new SimpleDateFormat("yyyy-MM-dd").format(cld.getTime());

        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<FundLedgerDetail> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getProjId() != null , FundLedgerDetail::getProjId,bo.getProjId());
		lqw.eq(bo.getAccountId() !=null, FundLedgerDetail::getAccountId,bo.getAccountId());
		lqw.like(StrUtil.isNotBlank(bo.getProjName()), FundLedgerDetail::getProjName, bo.getProjName());
        lqw.between(bo.getTransactionDate()!= null,
            FundLedgerDetail::getTransactionDate ,date, endDate);
        lqw.like(StrUtil.isNotBlank(bo.getAccountName()), FundLedgerDetail::getAccountName, bo.getAccountName());
        lqw.like(StrUtil.isNotBlank(bo.getSubjectName()), FundLedgerDetail::getSubjectName, bo.getSubjectName());
        lqw.eq(bo.getTransactionCategory() != null, FundLedgerDetail::getTransactionCategory, bo.getTransactionCategory());
        return lqw;
    }

}
